!define SOURCE "C:\Users\matthew\workspace\hhmodelmanager\src"

!include "MUI.nsh"
!include LogicLib.nsh
!include nsDialogs.nsh

!define PRODUCT_NAME "SWMMR"
!define PRODUCT_VERSION "0.1"
!define PRODUCT_PUBLISHER "CDMSmith / Philadelphia Office of Watersheds"
!define PRODUCT_WEB_SITE "http://swmmr.googlecode.com"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\install_swmmr.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"
; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!insertmacro MUI_PAGE_LICENSE "license.txt"
; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
page custom NewOrExistingPage NewOrExistingLeave
page custom InstallPage InstallPageLeave
; Finish page
page custom AuthenticatePage AuthenticatePageLeave
!define MUI_FINISHPAGE_RUN "$INSTDIR\swmmr.exe"
!insertmacro MUI_PAGE_FINISH
; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES
; Language files
!insertmacro MUI_LANGUAGE "English"
; MUI end ------

; Custom page functions start ------
Var NewOrExistingDialog
Var Label
Var RadioExisting
Var RadioNew
Var chose_existing
Var chose_new
Var AuthenticateDialog
Var InstallDialog
Var LabelServer
Var LabelLogin
Var LabelPassword
Var TextServer
Var WinAuthCheck
Var TextLogin
Var TextPassword
Var server
Var login
Var password
Var usewinauth
Var swmmr_installed
Var connectcmds
Var server_str

LangString NEWOREXISTING_TITLE ${LANG_ENGLISH} "Setup Complete"
LangString NEWOREXISTING_SUBTITLE ${LANG_ENGLISH} "Connect to an existing database or install a new one."
Function NewOrExistingPage
 !insertmacro MUI_HEADER_TEXT $(NEWOREXISTING_TITLE) $(NEWOREXISTING_SUBTITLE)
 nsDialogs::Create /NOUNLOAD 1018
 Pop $NewOrExistingDialog
 ${If} $NewOrExistingDialog == error
  Abort
 ${EndIf}
 ${NSD_CreateRadioButton} 0u 15u 100% 20u "Connect to an existing database"
 Pop $RadioExisting
 ${NSD_SetState} $RadioExisting 1
 ${NSD_CreateRadioButton} 0 30u 100% 20u "Install a new database on a local or networked SQLServer instance"
 Pop $RadioNew
 nsDialogs::Show
FunctionEnd

Function NewOrExistingLeave
 ${NSD_GetState} $RadioExisting $chose_existing
 ${NSD_GetState} $RadioNew $chose_new
FunctionEnd

LangString INSTALL_TITLE ${LANG_ENGLISH} "SWMM Repository Install"
LangString INSTALL_SUBTITLE ${LANG_ENGLISH} "Enter the server name and admin credentials for the host."
Function InstallPage
 !insertmacro MUI_HEADER_TEXT $(INSTALL_TITLE) $(INSTALL_SUBTITLE)
  StrCpy $swmmr_installed 0
 ${If} $chose_existing = ${BST_CHECKED}
  Abort
 ${EndIf}
 
 nsDialogs::Create /NOUNLOAD 1018
 Pop $InstallDialog
 ${If} $InstallDialog == error
  Abort
 ${EndIf}
 
 ${NSD_CreateLabel} 0 1u 35u 10u "Server:"
 Pop $LabelServer
 ${NSD_CreateText} 50u 0u 250u 12u ""
 Pop $TextServer
 ${NSD_CreateLabel} 0 16u 45u 10u "Admin Login:"
 Pop $LabelLogin
 ${NSD_CreateText} 50u 16u 250u 12u ""
 Pop $TextLogin
 ${NSD_CreateLabel} 0 31u 35u 10u "Password:"
 Pop $LabelPassword
 ${NSD_CreatePassword} 50u 31u 250u 12u ""
 Pop $TextPassword
 ${NSD_CreateLabel} 0 47u 100% 12u "Note: This will overwrite any existing SWMMR database on the server."
 Pop $Label
 GetDlgItem $0 $HWNDPARENT 1 ; Next button
 SendMessage $0 ${WM_SETTEXT} 0 "STR:Install"
 nsDialogs::Show
FunctionEnd

Function InstallPageLeave
 ${NSD_GetText} $TextServer $server
 ${NSD_GetText} $TextLogin $login
 ${NSD_GetText} $TextPassword $password
 ;ExecWait '"$INSTDIR\swmmrcmd.exe" --install -u $login -p $password -s $server"'
 nsExec::ExecToStack '"$INSTDIR\swmmrcmd.exe" --install -u $login -p $password -s $server"'
 Pop $0
 Pop $1
 StrCpy $swmmr_installed 0
 IntCmp $0 0 Success
  GetDlgItem $0 $HWNDPARENT 1 ; Next button
  SendMessage $0 ${WM_SETTEXT} 0 "STR:Retry"
  MessageBox MB_YESNO|MB_ICONSTOP "SWMMR installation on $server encountered the following error. Do you want to skip this part of the installation?$\r$\n$\r$\n$1" IDYES Skip
 Abort
 Success:
  MessageBox MB_OK "SWMMR successfully installed on $server"
  StrCpy $swmmr_installed 1
 Skip:
FunctionEnd

LangString AUTH_TITLE ${LANG_ENGLISH} "Connect to an Existing Database"
LangString AUTH_SUBTITLE ${LANG_ENGLISH} "Configure your connection"
Function AuthenticatePage
 !insertmacro MUI_HEADER_TEXT $(AUTH_TITLE) $(AUTH_SUBTITLE)
 nsDialogs::Create /NOUNLOAD 1018
 Pop $AuthenticateDialog

 ${If} $AuthenticateDialog == error
  Abort
 ${EndIf}

 ${NSD_CreateLabel} 0 1u 25u 10u "Server:"
 Pop $LabelServer
 ${If} $swmmr_installed == 1
  StrCpy $server_str "$server"
 ${Else}
  StrCpy $server_str ""
 ${EndIf}
 ${NSD_CreateText} 40u 0u 250u 12u "$server_str"
 Pop $TextServer
 ${NSD_CreateCheckBox} 40u 15u 70% 10u "Use Windows Authentication"
 Pop $WinAuthCheck
 ${NSD_SetState} $WinAuthCheck 1
 ${NSD_OnClick} $WinAuthCheck OnWinAuthCheck
 ${NSD_CreateLabel} 0 27u 25u 10u "Login:"
 Pop $LabelLogin
 EnableWindow $LabelLogin 0
 ${NSD_CreateText} 40u 26u 250u 12u ""
 Pop $TextLogin
 EnableWindow $TextLogin 0
 ${NSD_CreateLabel} 0 42u 35u 10u "Password:"
 Pop $LabelPassword
 EnableWindow $LabelPassword 0
 ${NSD_CreatePassword} 40u 41u 250u 12u ""
 Pop $TextPassword
 EnableWindow $TextPassword 0
 nsDialogs::Show
FunctionEnd

Function OnWinAuthCheck
 Pop $WinAuthCheck
 ${NSD_GetState} $WinAuthCheck $0
 ${If} $0 == 0
 ; enable username and password textboxes
  EnableWindow $LabelLogin 1
  EnableWindow $TextLogin 1
  EnableWindow $LabelPassword 1
  EnableWindow $TextPassword 1
 ${Else}
  ; disable username and password textboxes
  EnableWindow $LabelLogin 0
  EnableWindow $TextLogin 0
  EnableWindow $LabelPassword 0
  EnableWindow $TextPassword 0
 ${EndIf}
FunctionEnd

Function AuthenticatePageLeave
 ${NSD_GetText} $TextServer $server
 ${NSD_GetText} $TextLogin $login
 ${NSD_GetText} $TextPassword $password
 ${NSD_GetState} $WinAuthCheck $usewinauth
 
 StrCpy $connectcmds "--test -s $server"
 ${If} $usewinauth == 0
  StrCpy $connectcmds "$connectcmds -u $login -p $password"
 ${EndIf}
 
 nsExec::ExecToStack '"$INSTDIR\swmmrcmd.exe" $connectcmds' ; test connection
 Pop $0
 Pop $1
 IntCmp $0 0 Success
 GetDlgItem $0 $HWNDPARENT 1 ; Next button
 SendMessage $0 ${WM_SETTEXT} 0 "STR:Retry"
 MessageBox MB_YESNO|MB_ICONSTOP "The following error was encountered. Do you want to skip this part of the installation?$\r$\n$\r$\n$1" IDYES Skip
 Abort
 Success:
 Skip:
FunctionEnd

; Custom page functions end ----

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "swmmr_setup.exe"
InstallDir "$PROGRAMFILES\SWMMR"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show

Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite try
  File "${SOURCE}\buildfiles\dist\license.txt"
  File "${SOURCE}\buildfiles\dist\swmmr.exe"
  CreateDirectory "$SMPROGRAMS\SWMMR"
  CreateShortCut "$SMPROGRAMS\SWMMR\SWMMR.lnk" "$INSTDIR\swmmr.exe"
  CreateShortCut "$DESKTOP\SWMMR.lnk" "$INSTDIR\swmmr.exe"
  File "${SOURCE}\buildfiles\dist\swmmrcmd.exe"
SectionEnd

Section -AdditionalIcons
  WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  CreateShortCut "$SMPROGRAMS\SWMMR\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
  CreateShortCut "$SMPROGRAMS\SWMMR\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\swmmr.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\swmmr.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  Delete "$INSTDIR\${PRODUCT_NAME}.url"
  Delete "$INSTDIR\uninst.exe"
  Delete "$INSTDIR\swmmrcmd.exe"
  Delete "$INSTDIR\swmmr.exe"
  Delete "$INSTDIR\license.txt"

  Delete "$SMPROGRAMS\SWMMR\Uninstall.lnk"
  Delete "$SMPROGRAMS\SWMMR\Website.lnk"
  Delete "$DESKTOP\SWMMR.lnk"
  Delete "$SMPROGRAMS\SWMMR\SWMMR.lnk"

  RMDir "$SMPROGRAMS\SWMMR"
  RMDir "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  SetAutoClose true
SectionEnd